import Vue from 'vue'
import Vuex from 'vuex'
import router from '@/router'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    actIndex: '', // 默认打开最有一次
    list: [], // 缓存菜单列表
    incNames: [] // keep-alive页面
  },
  mutations: {
    init (state) {
      const list = Vue.prototype.$storage.get('list') === null ? [] : Vue.prototype.$storage.get('list')
      const actIndex = Vue.prototype.$storage.get('actIndex') === null ? '' : Vue.prototype.$storage.get('actIndex')
      const incNames = Vue.prototype.$storage.get('incNames') === null ? [] : Vue.prototype.$storage.get('incNames')
      if (list.length === 0 || actIndex === '' || incNames.length === 0) {
        router.push('/home').catch(e => { })
      } else {
        router.push(actIndex).catch(e => { })
      }
      state.list = list
      state.actIndex = actIndex
      state.incNames = incNames
      Vue.prototype.$storage.set('list', list)
      Vue.prototype.$storage.set('actIndex', actIndex)
      Vue.prototype.$storage.set('incNames', incNames)
    },
    reset (state) {
      state.list = []
      state.actIndex = ''
      state.incNames = []
      Vue.prototype.$storage.set('list', [])
      Vue.prototype.$storage.set('actIndex', '')
      Vue.prototype.$storage.set('incNames', [])
      router.push('/home').catch(e => { })
    },
    ascPath (state, data) {
      if (state.actIndex !== data.router) {
        const save = {
          title: data.name,
          router: data.router,
          component: data.component
        }
        const i = state.list.findIndex(item => item.router === data.router)
        if (i === -1) {
          state.list.push(save)
          state.incNames.push(data.component)
        }
        state.actIndex = data.router
        Vue.prototype.$storage.set('list', state.list)
        Vue.prototype.$storage.set('actIndex', state.actIndex)
        Vue.prototype.$storage.set('incNames', state.incNames)
        router.push(data.router).catch(e => { })
      }
    },
    descPath (state, data) {
      let actIndex = ''
      const tabs = state.list
      tabs.some((item, index) => {
        if (item.router === data) {
          const nextTab = tabs[index + 1] || tabs[index - 1]
          if (nextTab) {
            actIndex = nextTab.router
          } else {
            actIndex = ''
          }
          state.incNames.splice(state.incNames.indexOf(item.component), 1)
          return true
        }
      })
      state.actIndex = actIndex
      state.list = tabs.filter(tab => tab.router !== data)
      Vue.prototype.$storage.set('actIndex', state.actIndex)
      Vue.prototype.$storage.set('list', state.list)
      Vue.prototype.$storage.set('incNames', state.incNames)
      if (state.actIndex === '') {
        router.push('/home').catch(e => { })
      } else {
        state.list.some(item => {
          if (item.router === actIndex) {
            router.push(item.router).catch(e => { })
            return true
          }
        })
      }
    },
    tabClick (state, data) {
      state.actIndex = data
      Vue.prototype.$storage.set('actIndex', data)
      router.push(data).catch(e => { })
    },
    change (state, data) {
      state.actIndex = data
      Vue.prototype.$storage.set('actIndex', data)
    }
  },
  actions: {},
  modules: {}
})
